import json

str1 = '''
<tr>
<th scope="row">{{INDEX}}</th>
<td>{{TARGET}}</td>
<td>{{SOURCE}}</td>
<td>{{SCORE}}</td>
</tr>
'''


# 读取文件
def read_file(url):
    with open(url, "r", encoding='utf-8') as f:
        return f.read()


# 一行一行读取文件
def read_file_by_line(url):
    res = []
    with open(url, "r", encoding='utf-8') as f:
        for line in f.readlines():
            res.append(line.strip())
    return res


# 写文件
def write_file(url, data):
    with open(url, "w", encoding='utf-8') as f:
        f.write(data)


# 转换函数，将相似度转为HTML格式
def convert(output):
    print('output:', output)
    all = read_file("templates/template.html")
    body = ""
    # 读取模型输出json_out.json
    with open("out/json_out.json", "r") as f:
        data = json.load(f)
        for idx, d in enumerate(data):
            # print(d)
            line = str1
            line = line.replace("{{INDEX}}", str(idx + 1))
            line = line.replace("{{TARGET}}", d['target'])
            line = line.replace("{{SOURCE}}", d['source'])
            line = line.replace("{{SCORE}}", d['score'])
            body += line
    all = all.replace("{{TBODY}}", body)
    # print(all)
    # 将结果写到文件中
    write_file(output, all)


if __name__ == '__main__':
    convert()
